package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.annotation.TingShuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;

    //http://localhost:8500/api/user/wxLogin/wxLogin/0e1DmJ0w3NAUc43bCa0w3oEz163DmJ0
    @GetMapping("/wxLogin/{code}")
    public Result wxLogin(@PathVariable(value = "code") String code) {
        Map<String, Object> map = userInfoService.wxLogin(code);
        return Result.ok(map);
    }

    //前端不会来调用,后添加的,需要修改前端代码逻辑
    @GetMapping("/refreshToken")
    public Result refresh() {
        Map<String, Object> map = userInfoService.refresh();
        return Result.ok(map);
    }

    // http://localhost:8500/api/user/wxLogin/getUserInfo
    @GetMapping("/getUserInfo")
    @TingShuLogin
    public Result getUserInfo() {
        Long userId = AuthContextHolder.getUserId();
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }

    //http://localhost:8500/api/user/wxLogin/updateUser
    @PostMapping ("/updateUser")
    @TingShuLogin
    public Result updateUser(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.updateUser(userInfoVo);
        return Result.ok();
    }
}
